/*
 * @(#)SpsFaultPolicy.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.ognl;

import java.util.Locale;

import com.nhncorp.usf.core.exception.CallExecutorException;

/**
 * The Class SpsFaultPolicy.
 * 
 * @author MiddleWare Platform Development Team
 */
public final class SpsFaultPolicy {
	public static final String INVALID_MODULE = "INVALID MODULE] ";
	public static final String INVALID_RESOURCE = "INVALID RESOURCE] ";
	public static final String INVALID_PROCEDURE = "INVALID PROCEDURE] ";
	public static final String INVALID_PARAMETER = "INVALID PARAMETER] ";
	public static final String INVALID_CALL = "INVALID CALL] ";

	/**
	 * Instantiates a new sps fault policy.
	 */
	private SpsFaultPolicy() {
		throw new UnsupportedOperationException();
	}

	/**
	 * Gets the code.
	 * 
	 * @param message the message
	 * 
	 * @return the code
	 */
	private static String getCode(String message) {
		if (message.startsWith(INVALID_MODULE)) {
			return "101";
		}

		if (message.startsWith(INVALID_RESOURCE)) {
			return "102";
		}

		if (message.startsWith(INVALID_PROCEDURE)) {
			return "103";
		}

		if (message.startsWith(INVALID_PARAMETER)) {
			return "104";
		}

		if (message.startsWith(INVALID_CALL)) {
			return "105";
		}

		return "107";
	}

	/**
	 * Gets the code.
	 * 
	 * @param throwable the throwable
	 * 
	 * @return the code
	 */
	public static String getCode(Throwable throwable) {
		if (throwable instanceof IllegalArgumentException || throwable instanceof CallExecutorException) {
			String message = throwable.getMessage();

			if (message == null) {
				// INVALID_MISC
				return "107";
			} else {
				return getCode(message.toUpperCase(Locale.US));
			}
		}

		return "E106"; // user exception
	}
}
